﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PokeData;
using System.IO;

namespace PokeTD_MapEditor
{
    public partial class CreepEdit : Form
    {
        public CreepEdit(WavesDS myWDS)
        {
            InitializeComponent();
            wds = myWDS;
        }
        WavesDS wds;

        private string _fileName = "";

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        private void nurZahlen(object sender, KeyEventArgs e)
        {
            //Allow navigation keyboard arrows
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.Down:
                case Keys.Left:
                case Keys.Right:
                case Keys.PageUp:
                case Keys.PageDown:
                    e.SuppressKeyPress = false;
                    return;
                default:
                    break;
            }

            //Block non-number characters
            char currentKey = (char)e.KeyCode;
            bool modifier = e.Control || e.Alt || e.Shift;
            bool nonNumber = char.IsLetter(currentKey) ||
                             char.IsSymbol(currentKey) ||
                             char.IsWhiteSpace(currentKey) ||
                             char.IsPunctuation(currentKey);

            if (!modifier && nonNumber)
                e.SuppressKeyPress = true;

            //Handle pasted Text
            if (e.Control && e.KeyCode == Keys.V)
            {
                //Preview paste data (removing non-number characters)
                string pasteText = Clipboard.GetText();
                string strippedText = "";
                for (int i = 0; i < pasteText.Length; i++)
                {
                    if (char.IsDigit(pasteText[i]))
                        strippedText += pasteText[i].ToString();
                }

                if (strippedText != pasteText)
                {
                    //There were non-numbers in the pasted text
                    //e.SuppressKeyPress = true;

                    //OPTIONAL: Manually insert text stripped of non-numbers
                    TextBox me = (TextBox)sender;
                    int start = me.SelectionStart;
                    string newTxt = me.Text;
                    newTxt = newTxt.Remove(me.SelectionStart, me.SelectionLength); //remove highlighted text
                    newTxt = newTxt.Insert(me.SelectionStart, strippedText); //paste
                    me.Text = newTxt;
                    me.SelectionStart = start + strippedText.Length;
                }
                else
                    e.SuppressKeyPress = false;
            }
        }

        private void saveToDatabase(object sender, EventArgs e)
        {
            int sel = lbCreeps.SelectedIndex;

            var cr = from ce in wds.Creeps
                     where ce.id == (int)(lbCreeps.SelectedItem as ComboboxItem).Value
                     select ce;

            WavesDS.CreepsRow c = cr.First<WavesDS.CreepsRow>();

            c.CreepName = txtName.Text;
            c.MoneyPerKill = int.Parse(txtMPK.Text);
            c.type1 = cbTyp1.SelectedIndex - 1;
            c.type2 = cbTyp2.SelectedIndex - 1;
            c.ImgPath = txtPath.Text;
            c.PhysVert = int.Parse(txtVert.Text);
            c.SpVert = int.Parse(txtSpVert.Text);
            c.MovementSpeed = int.Parse(txtMoveSpeed.Text);
            c.HP = int.Parse(txtHP.Text);

            refreshContainer(sel);
        }

        private void lbCreeps_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cr = from ce in wds.Creeps
                     where ce.id == (int)(lbCreeps.SelectedItem as ComboboxItem).Value
                     select ce;

            WavesDS.CreepsRow c = cr.First<WavesDS.CreepsRow>();

            txtName.Text = c.CreepName;
            txtMPK.Text = c.MoneyPerKill.ToString();
            cbTyp1.SelectedIndex = c.type1 + 1;
            cbTyp2.SelectedIndex = c.type2 + 1;
            txtPath.Text = c.ImgPath;
            txtVert.Text = c.PhysVert.ToString();
            txtSpVert.Text = c.SpVert.ToString();
            txtMoveSpeed.Text = c.MovementSpeed.ToString();
            txtHP.Text = c.HP.ToString();
            cbFlying.Checked = c.flying;
        }

        private void CreepEdit_Load(object sender, EventArgs e)
        {
            if (wds.Creeps.Rows.Count == 0)
                splitContainer1.Enabled = false;

            refreshContainer(0);
        }

        private void schließenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void refreshContainer(int sel)
        {
            lbCreeps.Items.Clear();
            var cs = from c in wds.Creeps
                     select c;

            foreach (WavesDS.CreepsRow c in cs)
            {
                lbCreeps.Items.Add(new ComboboxItem(c.CreepName, c.id));
            }
            lbCreeps.SelectedIndex = sel;
        }

        private void bttnNeu_Click(object sender, EventArgs e)
        {
            wds.Creeps.AddCreepsRow("Neues Creep", 0, -1, false, 0, 0, 1, "0", 1, 1);
            refreshContainer(lbCreeps.Items.Count);
        }

        private void bttnLoeschen_Click(object sender, EventArgs e)
        {
            var cr = from ce in wds.Creeps
                     where ce.id == (int)(lbCreeps.SelectedItem as ComboboxItem).Value
                     select ce;

            WavesDS.CreepsRow c = cr.First<WavesDS.CreepsRow>();

            c.Delete();
            refreshContainer(lbCreeps.Items.Count - 2);
        }

        
    }
}
